""" Base class for Tabs """

from qt import *

class TableWindow(QMainWindow):

    def __init__(self, parent=None, headers=(), rows=0):
        super(TableWindow, self).__init__(parent)

        table = self.table = QTableWidget(rows, len(headers))
        table.setHorizontalHeaderLabels(list(headers))
        table.setEditTriggers(QTableWidget.NoEditTriggers)
        table.setSelectionBehavior(QTableWidget.SelectRows)
        table.setSelectionMode(QTableWidget.SingleSelection)
        table.setAlternatingRowColors(True)

#        self.set_colors()

        table.verticalHeader().setVisible(False)
        table.verticalHeader().setDefaultSectionSize(16)
        table.horizontalHeader().setStretchLastSection(True)

        title = getattr(self, 'TITLE', None)
        if title:
            self.setWindowTitle(title)

        
        table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) # Auto-column-width
        self.setCentralWidget(table)

    def set_colors(self):
        palette = QPalette(QColor(48, 48, 48))
        palette.setColor(QPalette.Text, QColor(222, 222, 222))
        palette.setColor(QPalette.Base, QColor(48, 48, 48))
        palette.setColor(QPalette.AlternateBase, QColor(52, 52, 52))
        self.setPalette(palette)
        # TODO: Use setStyleSheet

    def add_item(self, *cols):
        irow = self.table.rowCount()
        self.table.insertRow(irow)
        for icol, col in enumerate(cols):
            if isinstance(col, (str, unicode)):
                item = QTableWidgetItem(col)
            else:
                item = col
            self.table.setItem(irow, icol, item)

    def delete_all_items(self):
        self.table.clearContents()
        self.table.setRowCount(0)
        

